package com.kkw.shoppingmall.coupon.service.impl;

import com.kkw.shoppingmall.coupon.entity.SeckillSkuRelationEntity;
import com.kkw.shoppingmall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kkw.common.utils.PageUtils;
import com.kkw.common.utils.Query;

import com.kkw.shoppingmall.coupon.dao.SeckillSessionDao;
import com.kkw.shoppingmall.coupon.entity.SeckillSessionEntity;
import com.kkw.shoppingmall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLates3DaySession() {
        //计算最近三天的时间
        //计算最近三天
//        Date date = new Date();
//        LocalDate now = LocalDate.now();
//        //加一天的时间
//        LocalDate plus = now.plusDays(1);
//        //加两天的时间
//        LocalDate plus2 = now.plusDays(2);
        //2021-06-21 00:00:00  2021-06-23 23:59:59  必须要弄成这样


        List<SeckillSessionEntity> list = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime(), endTime()));
        if (list != null && list.size() > 0) {
            List<SeckillSessionEntity> collect = list.stream().map(session -> {
                Long id = session.getId();
                List<SeckillSkuRelationEntity> relationEnties = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
                session.setRelationSkus(relationEnties);
                return session;
            }).collect(Collectors.toList());

            return collect;
        }
        return null;
    }

    private String startTime() {
        //计算最近三天
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        //起始时间 2021-06-21 00:00:00
        LocalDateTime start = LocalDateTime.of(now, min);
        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    private String endTime() {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.plusDays(2);
        //2021-06-23 23:59:59
        LocalDateTime end = LocalDateTime.of(localDate, LocalTime.MAX);
        String format = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

}